package cn.rjzjh.tapestry.tams.services;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.apache.tapestry5.hibernate.HibernateSessionSource;
import org.hibernate.c3p0.internal.C3P0ConnectionProvider;
import org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl;
import org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.engine.spi.SessionFactoryImplementor;

import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;

import cn.rjzjh.tapestry.mybatis.services.MybatisConfigurer;

public class MybatisConfigurerFormHb implements MybatisConfigurer {

	private final HibernateSessionSource hibernateSessionSource;

	public MybatisConfigurerFormHb(HibernateSessionSource hibernateSessionSource) {
		this.hibernateSessionSource = hibernateSessionSource;
	}

	@Override
	public void configure(Configuration configuration) {
		C3P0ConnectionProvider cp = (C3P0ConnectionProvider) ((SessionFactoryImplementor) hibernateSessionSource
				.getSessionFactory()).getConnectionProvider();
		DataSource dataSource = cp.unwrap(DataSource.class);
		TransactionFactory transactionFactory = new JdbcTransactionFactory();
		Environment environment = new Environment("development",
				transactionFactory, dataSource);
		configuration.setEnvironment(environment);
	}
}
